/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facade;

import entity.Account;
import entity.BankTransaction;
import entity.CurrencyEE;
import entity.Customer;
import entity.Groups;
import exceptions.LoginAlreadyExistsException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Vojtisek
 */
@Stateless
@RolesAllowed("manager")
public class ManagerBean implements ManagerLocal {

    @PersistenceContext
    private EntityManager em;
    @Resource
    private SessionContext ctx;

    public Account getAccount(Object id) throws Exception {
        return em.find(Account.class, id);
    }

    public Collection<Customer> getAllCustomers() throws Exception {
        return em.createQuery("select object(o) from Customer as o").getResultList();
    }

    public void createCustomer(Customer customer) throws LoginAlreadyExistsException, EJBException, Exception {
        try {
            if (findCustomer(customer.getLogin()) != null) {
                throw new LoginAlreadyExistsException();
            }
        } catch (NoResultException e) {
            Groups g = new Groups();
            g.setGroupName("obcan");
            g.setLogin(customer.getLogin());
            em.persist(customer);
            em.persist(g);
            Logger.getLogger(CustomerFacade.class.getName()).log(Level.INFO, "Customer created. login: " + customer.getLogin());
        }
    }

    public void editCustomer(Integer id, String login, String firstName, String lastName, BigDecimal limit, CurrencyEE currency) throws LoginAlreadyExistsException, Exception {
        Customer foo;
        Customer customer = getCustomer(id);
        try {
            foo = findCustomer(login);
            if (!foo.getId().equals(customer.getId())) {
                throw new LoginAlreadyExistsException();
            }
        } catch (LoginAlreadyExistsException e) {
            throw e;
        } catch (NoResultException ex) {
        }
        Groups g = (Groups) em.createQuery("SELECT o FROM Groups AS o WHERE o.login = :login").setParameter("login", customer.getLogin()).getSingleResult();
        g.setLogin(login);
        customer.setFirstName(firstName);
        customer.setLastName(lastName);
        customer.setLogin(login);
        customer.setTransactionLimit(limit);
        customer.setCurrency(currency);
        em.merge(customer);
    }

    public Collection<Account> getCustomerAccounts(Integer customerId) throws Exception {
        Customer cust = em.find(Customer.class, customerId);
        return cust.getAccounts();
    }

    public Customer getCustomer(Integer id) throws Exception {
        return em.find(Customer.class, id);
    }

    public Customer findCustomer(String login) throws EJBException, NoResultException, Exception {
        return (Customer) em.createQuery("select o from Customer as o where o.login = :login").setParameter("login", login).getSingleResult();
    }

    public List<BankTransaction> getAllTransactions() throws Exception {
        return em.createQuery("select object(o) from BankTransaction as o").getResultList();
    }

    public List<BankTransaction> getAccountTransactions(Integer accountId) throws Exception {
        return em.createQuery("SELECT o FROM BankTransaction AS o WHERE o.from.id = :accountId OR o.to.id = :accountId").setParameter("accountId", accountId).getResultList();
    }

    public List<Account> getAllAccounts() throws Exception {
        return em.createQuery("select object(o) from Account as o").getResultList();
    }
}
